﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Model;
using System.Data.SqlClient;
namespace Manager.Helper
{
    public class ResultFileCach
    {
        private static object o = new object();
        public static Queue<string> strList = new Queue<string>();
        static bool isexcute = false;
        public static int GetQueueCount()
        {
            return strList.Count;
        }
        public static string InQueue(string resultName)
        {
            if (strList.Contains(resultName))
                return "您之前上传过文件，还在处理当中，请等待...";
            else
            {
                strList.Enqueue(resultName);
                return string.Empty;
            }
        }
        public static void OutQueue()
        {
            strList.Dequeue();
        }
        public static void Start()
        {
            System.Threading.Thread thread = new System.Threading.Thread(new System.Threading.ThreadStart(Excute));
            thread.Start();
        }
        public static void Excute()
        {
            lock (o)
            {
                while (true)
                {
                    try
                    {
                        if (!isexcute)
                        {
                            isexcute = true;
                            UploadCath sqlfile = GetCath();
                            ExcelIO excel = ExcelIO.GetInstance();
                            var ds= excel.ImportExcel(sqlfile.FilePath);
                            
                            //插入数据库
                            try
                            {
                                Delete(sqlfile.id);
                            }
                            catch
                            {
                                //日记
                            }
                        }

                    }
                    catch
                    {
                        //日记
                        //isexcute = false;
                    }
                    finally
                    {
                        isexcute = false;
                    }
                    System.Threading.Thread.Sleep(1000 * 2);
                }
                
            }
        }
        public static void CheckExcel(string file)
        {

            ExcelIO ex = ExcelIO.GetInstance();
            bool exist = ex.IsExistExecl();
            if (!exist)
            {
                throw new Exception(
                 "无法创建Excel对象，可能您的计算机未安装Excel!"
                );
            }
            else
            {
                try
                {
                    
                        System.Data.DataSet ds = ex.ImportExcel(file);
                }
                catch 
                {
                    throw new Exception("您的excel格式不对，或者数据有错误请检查");
                }
            }
        }
        public static void Insert(UploadCath cath)
        {
            string sql = "insert into HW_UploadRecode values(@Schoolcode,@UpLoadUser,@State,@FilePath)";
            using (SqlConnection conn = new SqlConnection(Utility.WebConn.connString))
            {
                SqlParameter[] paras = new SqlParameter[]{
                    new SqlParameter("@Schoolcode",cath.Schoolcode),
                    new SqlParameter("@UpLoadUser",cath.UpLoadUser),
                    new SqlParameter("@State",cath.State),
                    new SqlParameter("FilePath",cath.FilePath),
                };
                SqlCommand comm = new SqlCommand(sql, conn);
                comm.Parameters.AddRange(paras);
                comm.ExecuteNonQuery();
            }

        }
        public static UploadCath GetCath()
        {
            string sql = "select top(1) * from HW_UploadRecode order by id";
            using (SqlConnection conn = new SqlConnection(Utility.WebConn.connString))
            {
               
                SqlCommand comm = new SqlCommand(sql, conn);
                SqlDataReader reader=comm.ExecuteReader();
                if (reader.Read())
                {
                    UploadCath cath = new UploadCath()
                    {
                        id=Convert.ToInt32(reader["id"].ToString()),
                        Schoolcode = reader["Schoolcode"].ToString(),
                        UpLoadUser = reader["UpLoadUser"].ToString(),
                        FilePath = reader["FilePath"].ToString(),
                        State = Convert.ToBoolean(reader["State"]),
                    };
                    return cath;
                }
                else
                    return null;
            
                
            }
        }
        public static void Delete(int id)
        {
            string sql = "delete from HW_UploadRecode where id=@id";
            using (SqlConnection conn = new SqlConnection(Utility.WebConn.connString))
            {
                SqlParameter[] paras = new SqlParameter[]{
                    new SqlParameter("@id",id),
                };
                SqlCommand comm = new SqlCommand(sql, conn);
                comm.Parameters.AddRange(paras);
                comm.ExecuteNonQuery();
            }
        }

    }
}
